import { ModuleForm } from "@/components/module-modal/types";
import { ModuleListVO } from "@/pojo/ModuleListVO";
import SettingsService from "@/service/settings";
import { CreateModuleBO } from "@/service/settings/types";
import { reactive, ref } from "vue";

export default function () {
  const showModuleModal = ref(false);
  const showModuleList = ref(false);

  const moduleList = ref<ModuleListVO[]>([]);
  const createModuleForm = reactive<ModuleForm>({});

  const handleShowCreateModule = () => {
    showModuleModal.value = true;
  };

  const handleShowModuleList = () => {
    showModuleList.value = true;
  };

  const handleConfirmCreateModule = (payload: ModuleForm) => {
    console.log(payload);
    const params: CreateModuleBO = {
      name: payload.name || "",
      slug: payload.slug || "",
      canDelete: payload.canDelete || 1,
    };
    SettingsService.createModule(params).then(() => {
      getModuleList();
    });
  };

  const getModuleList = () => {
    SettingsService.getModulesList()?.then((res) => {
      moduleList.value = res?.data || [];
    });
  };

  return {
    showModuleModal,
    showModuleList,
    moduleList,
    createModuleForm,
    getModuleList,
    handleShowCreateModule,
    handleShowModuleList,
    handleConfirmCreateModule,
  };
}
